<div class="data-input">
    <fieldset>
        <legend><?php echo "$formTitle";?></legend>
        <?php
          echo form_open_multipart($subLink, "");
        ?>
            <table width="100%">
               <tr>
                   <td valign="top" width="50%">
                       <?php
                         foreach ($dataInput as $key => $row);
                       ?>
                       <label>Nama</label><?php echo form_input("nama", $row['member']['member'], "id='nama'");?>
                       <fieldset class="field-group">
                           <label>Tgl. Lahir</label>
                           <?php echo form_input("tglLahir", datefmysql($row['member']['tgl_lahir']), "class='tanggal datepicker-input' id='tglLahir'");?>
                       </fieldset>
                       <fieldset class="field-group">
                           <label>Jenis Kelamin</label>
                           <?php
                             if($row['member']['jenis_kelamin'] == "L"){
                                 $P = false;
                                 $L = true;
                             }else{
                                 $P = true;
                                 $L = false;
                             }
                           ?>
                           <label class="field-option"><?php echo form_radio("jeKel", "L",$L,"id='laki-laki'")?>Laki-laki</label>
                           <label class="field-option"><?php echo form_radio("jeKel", "P",$P,"id='perempuan'")?>Perempuan</label>
                       </fieldset>
                       <label>Alamat</label><?php echo form_input("alamat", $row['member']['alamat'], "id='alamat'");?>
                       <label>No. Telepon</label><?php echo form_input("noTelp", $row['member']['no_telp'], "id='noTelp' maxsize='15'");?>
                       <label>Pekerjaan</label>
                       <select name="pekerjaan" id="pekerjaan">
                            <option value="">Pilih pekerjaan</option>
                            <?php
                                 foreach ($listPekerjaan as $pkj){
                            ?>        
                                <option value="<?php echo $pkj['id'];?>" <?php if($row['member']['id_pekerjaan'] == $pkj['id']) echo "selected=selected";?>><?php echo $pkj['nama'];?></option>
                            <?php     
                                 }
                            ?>
                       </select>
                   </td>
                   <td valign="top">
                       <?php echo form_button("", "Tambah", "class='tambahBaris'");?>
                       <div class="jenisMember">
                           <?php
                                $no = 1;
                                foreach ($row['jenis_keanggotaan'] as $rows){
                           ?>
                               <fieldset class="field-group jenisMemberList jenis_keanggotaan_br">
                                   <label>Jenis Member</label><span style="font-size: 11px;padding-top: 5px;width: 24em;" class="jenis_member"><?php echo "$rows[jenis_member]";?></span>
                                   <input type="hidden" value="<?php echo $rows['id_jenis_keanggotaan'];?>" id="id_jenis_keanggotaan<?php echo "$no";?>"/>
                                   <?php
                                     echo form_button("", "Hapus", "class='del_button' onClick=hapusKeanggotaan($no,this)");
                                   ?>
                               </fieldset>
                           <?php
                                $no++;
                                }
                           ?>
                           <fieldset class="field-group jenisMemberList baris">
                                <label>Jenis Member</label>
                                <select name="jenisMember[]" id="jenisMember">
                                        <option value="">Pilih jenis member</option>
                                    <?php
                                        foreach ($listJenisMember as $jenisMember){
                                    ?>
                                        <option value="<?php echo $jenisMember['id'];?>"><?php echo $jenisMember['nama'];?></option>
                                    <?    
                                        }
                                    ?>
                                </select>  
                            </fieldset>
                       </div>
                       <fieldset class="field-group">
                          <label>Foto</label><?php echo form_upload('userfile');?>
                       </fieldset>
                       <fieldset class="input-process">
                           <?php
                              echo form_hidden('idMember', $row['member']['id']);
                              echo form_submit("edit", "Simpan", "class='button' style='margin-right:2px'");
                              echo form_button('Batal', 'Batal', 'class=button onClick='."javascript:location.href='".base_url()."index.php/member/'".'');
                            ?>
                       </fieldset>
                   </td>
               </tr>
            </table>
        <?php
          echo form_close();
        ?>
    </fieldset>
</div>
<script type="text/javascript">
    $(document).ready(function(){ 
        baris = $('.jenisMemberList').length;
        var max = <?php echo "$countJenisMember";?>;
        $('.tambahBaris').click(function(){
            if(baris < max){
                var string = "<fieldset class='field-group baris'>"+
                    "<label>Jenis Member</label>"+
                    "<select name='jenisMember[]'>"+
                    "<option value=''>Pilih jenis member</option>"+
                       <?php
                         foreach ($listJenisMember as $rowz){
                       ?>
                            "<option value='<?php echo $rowz['id'];?>'><?php echo $rowz['nama'];?></option>"+           
                       <?php                
                         }
                       ?>
                    "</select>"+
                    "<input type='button' onClick='removeMe(this)' class='del_button' value='Hapus'>"+
                    "</fieldset>";
                $('.jenisMember').append(string);
            }
            baris++;
        })
    })
    
    function removeMe(el){
        var parent = el.parentNode;
        parent.parentNode.removeChild(parent);
        baris = 1 + $('.jenis_keanggotaan_br').length;;
    }
    function hapusKeanggotaan(count,el){
        var ok = confirm('Apakah anda yakin akan menghapus data ini?');
        if(!ok){
            return false;
        }else{
            var idKeanggotaan = $('#id_jenis_keanggotaan'+count).val();
            $.ajax({
                url: "<?php echo site_url('ajax_target/hapus_keanggotaan');?>"+"/"+idKeanggotaan,
                cache: false,
                type: 'post',
                dataType: 'json',
                success: function(msg){
                    if(!msg.status){
                        alert('Proses penghapusan gagal, data digunakan untuk transaksi lain');
                    }else{
                        alert('Data berhasil dihapus');
                          location.reload();
                    }
                }
            })
        }
    }
</script>
